define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Comment, Auth, $timeout, GoPage, GoDialog) {
		
		$scope.$on('$ionicView.afterEnter', function(){
			$scope.init_data();
		},false)
		
		//初始化数据
		$scope.init_data = function() {
			$scope.firstHide = false;
			Comment.discuss_list(1, function(data) {
				$scope.datas = data.discuss;
				$scope.tPage = data.page.tPage;
				$scope.firstHide = true;
				console.log(data)
			})			
		}
		
		//查看其它人的回复
		//记录是否是上一次打开的回复
		var is_showother;
		$scope.arrowimg1 = true;
		$scope.arrowimg2 = true;
		$scope.showOtherCom = function($event, id, is_star) {
			var otherComs = document.getElementsByClassName("otherComs");
			for(var i = 0; i < otherComs.length; i++) { //先隐藏所以
				otherComs[i].style.display = "none";
			}
			if(is_showother == id) {
				is_showother = "";
				if(is_star == 1) {
					$scope.arrowimg1 = true;
				} else {
					$scope.arrowimg2 = true;
				}
				return;
			}
			var child = $event.target;
			var childs;
			if(child.nodeName == "IMG") {
				childs = child.parentNode.parentNode.children; //li下所有的子元素
			} else {
				childs = child.parentNode.children; //li下所有的子元素
			}
			for(var i = 0; i < childs.length; i++) { //显示当前
				if(childs[i].nodeName == "DIV" && childs[i].className.indexOf("otherComs") != -1) {
					childs[i].style.display = "block";
					is_showother = id;
					if(is_star == 1) {
						$scope.arrowimg1 = false;
					} else {
						$scope.arrowimg2 = false;
					}
				}
			}
		}

		/*===========下拉刷新 =============*/
		$scope.nPage = 1;
		$scope.isLoad = false;
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Comment.discuss_list(1, function(data) {
				if(data != null) {
					$scope.datas = data.discuss;
				} else {
					$scope.datas = new Object();
				}
				$scope.nPage = 1;
			},
			function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			if($scope.nPage < $scope.tPage) {
				$scope.isLoad = true;
				Comment.discuss_list($scope.nPage + 1, function(data) {
					if(data != null) {
						if($scope.datas != undefined) {
							$scope.datas = $scope.datas.concat(data.discuss);
							$scope.nPage += 1;
						} else {
							$scope.datas = data.discuss;
						}
					}
				},
				function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
			}
		};
		/*===========下拉刷新 =============*/

		//页面跳转
		$scope.onshow_first = function(store_id) {
			GoPage.go_states("first", {
				"store_id": store_id
			});
		};
		$scope.go_product = function(goods_id) {
			GoPage.go_states("productdetail", {
				"goods_id": goods_id,
				"type": "goods"
			})
		};
		
		//图片放大
		$scope.showBigImg = function(imgUrl) {
			GoDialog.showBigImg(imgUrl);
		}
		
		//按钮事件，根据按钮id执行不同的事件
		$scope.buttonEvent = function(id, comment_id) {
			if(id == 1) {
				GoPage.go_states("aftalkagain", {
					"id": comment_id,
					"type":1
				});
			} else if(id == 2) {//改为好评
				GoDialog.confirm("提示","改为好评的机会只有一次，且修改后无法恢复，确定吗？",function(){
					Comment.change_comment(comment_id, function(data) {
						if(data.success){
							GoDialog.toast("修改成功！");
							$scope.init_data();
						}else{
							GoDialog.alert("提示",data.msg);
						}
					})
				})
			} else if(id == 3) {//删除
				GoDialog.confirm("提示","评价删除将不可恢复，且不可再追加评论，确定吗？",function(){
					Comment.delete_comment(comment_id, function(data) {
						if(data.success){
							GoDialog.toast("删除成功！");
							$scope.init_data();
						}else{
							GoDialog.alert("提示",data.msg);
						}
					})
				})
			}
		}
		

	}

	controller.$inject = ['$scope', 'Comment', 'Auth', '$timeout', 'GoPage', 'GoDialog'];

	return app.registerController("commentctrl", controller);
});